package com.XinYun.Library.modules.Register.Servlet;

import com.XinYun.Library.modules.Register.Service.Impl.AllServiceImpl;
import com.XinYun.Library.modules.Register.Service.Impl.UserServiceImpl;
import com.XinYun.Library.modules.Register.Service.UserService;
import com.XinYun.Library.modules.commons.ResponseWrapper.ResponseWrapper;
import com.XinYun.Library.utils.FormatUtil.FormatUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserServiceImpl userService = new UserServiceImpl();
        AllServiceImpl allService = new AllServiceImpl();
        BufferedReader reader = request.getReader();
        String readerLine = reader.readLine();
        JSONObject jsonObject = JSON.parseObject(readerLine);
        String intername = jsonObject.getString("Register_name");
        String sid = jsonObject.getString("Register_account");
        String password = jsonObject.getString("Register_password");
        String mail = jsonObject.getString("Register_mail");

        HttpSession session = request.getSession();

        String rcode = jsonObject.getString("Register_rcode");
        //得到前端 的 验证码 rcode
        //String s1 = "A12345";
        //session.setAttribute("rcode", s1);
        String rcodes = (String)session.getAttribute("EmailCode");
        //String rcodes = "A12345";
        try {
            if (userService.isExist(sid)) {
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.ExistFail();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //request.getRequestDispatcher("Register.jsp").forward(request,response);
            }else if(!FormatUtil.isEmail(mail)){
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.MailFail();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //request.getRequestDispatcher("Register.jsp").forward(request,response);
            }else if(!rcode.equals(rcodes)){
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.CodeFail();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //request.getRequestDispatcher("Register.jsp").forward(request,response);
            } else if(allService.isRegister(intername,sid,password,mail) && !StringUtils.isNullOrEmpty(rcode) && rcode.equals(rcodes)){
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.NoData();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //request.getRequestDispatcher("Login.jsp").forward(request,response);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
